package wcg;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;

import javax.swing.JComboBox;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Toolkit;
import javax.swing.JLabel;

import GraphicView.GraphicView;

import java.awt.Font;
import java.awt.Insets;

public class Optionconsole extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JComboBox jComboBoxIn = null;
	private JScrollPane jScrollPane1 = null;
	private JTextArea jTextAreaOut = null;
	private HashMap<String, OptioncommandInterface> Parent = new HashMap<String,OptioncommandInterface>();;
	private LinkedList<String> ParentID = new LinkedList<String>();
	private JPanel panel;
	private JLabel lblNewLabel;
	private String LevelStr = "";
	/**
	 * @param owner
	 */
	public Optionconsole(Frame owner) {
		super(owner);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Optionconsole.class.getResource("/images/application_osx_terminal.png")));
		initialize();
	}
	public void setParent(OptioncommandInterface pa){
		Parent.put("/",pa);
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(561, 228);
		this.setTitle("command for debug");
		this.setContentPane(getJContentPane());
		jComboBoxIn.setEditable(true);
		jComboBoxIn.setEnabled(true);
		jComboBoxIn.setFocusable(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPanel(), BorderLayout.SOUTH);
			jContentPane.add(getJScrollPane1(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jComboBoxIn	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxIn() {
		if (jComboBoxIn == null) {
			jComboBoxIn = new JComboBox();
			jComboBoxIn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String cstr = e.getActionCommand();
					if(cstr.compareTo("comboBoxEdited")==0){
						String str=jComboBoxIn.getSelectedItem().toString();
						if(!str.isEmpty()){
							jComboBoxIn.setSelectedItem(null);
							//Print(str+"\n");
							jComboBoxIn.insertItemAt(str, 0);
							str = str.trim();
							String[] tok = getCommand(str);
							if(tok.length>=1){
								String[] cmd = tok[0].split("/");
								String level = null;
								if(cmd.length==1) level = "/";
								else              level = cmd[0];
								OptioncommandInterface o = Parent.get(level);
								if(o != null){
									String c = cmd[cmd.length-1];
									for(int i=1;i<tok.length;i++){
										c = c + " " + tok[i];
									}
									LevelStr = level+"/";
									o.OnCommand(c);
								}
							}
						}
					}
				}
			});
			jComboBoxIn.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					//System.out.println("itemStateChanged()"); 
				}
			});
		}
		return jComboBoxIn;
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getJTextAreaOut());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes jTextAreaOut	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreaOut() {
		if (jTextAreaOut == null) {
			jTextAreaOut = new JTextArea();
			jTextAreaOut.setLineWrap(true);
			jTextAreaOut.setMargin(new Insets(2, 8, 8, 2));
			jTextAreaOut.setFont(new Font("Monospaced", Font.PLAIN, 12));
			jTextAreaOut.setEditable(false);
			jTextAreaOut.setText("");
		}
		return jTextAreaOut;
	}
	public void print(String addstr){
		jTextAreaOut.setText(jTextAreaOut.getText()  + LevelStr + addstr);
	}
	// コマンド文字をトークンに分解して返す
	public static String[] getCommand(String cmd){
		cmd.trim();
		cmd.toLowerCase();
		if(cmd.isEmpty()) return null;
		StringTokenizer tok = new StringTokenizer(cmd," ");
		int tokcount = tok.countTokens();
		String[] cm = new String[tokcount];
		int i=0;
		while(tok.hasMoreTokens()){
			cm[i++] = tok.nextToken();
		}
		return cm;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getJComboBoxIn(), BorderLayout.CENTER);
			panel.add(getLblNewLabel(), BorderLayout.WEST);
		}
		return panel;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Command>");
		}
		return lblNewLabel;
	}
	/**
	 * @param graphicView
	 */
	public void addParent(OptioncommandInterface p,String prompt) {
		Parent.put(prompt,p);
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
